/*
 * PopupUpdateManager.hpp
 *
 *  Created on: Nov 20, 2012
 *      Author: guillou
 */

#ifndef POPUPUPDATEMANAGER_HPP_
#define POPUPUPDATEMANAGER_HPP_

#include <QMutex>
#include <QThread>
#include <QTimer>
#include <curl/curl.h>
#include <string>
#include <vector>


struct PopupUpdate
{
  PopupUpdate(float p_version);
  ~PopupUpdate() {}

  float version() const;
  const std::string & basedir() const;
  const std::string & execPath() const;
  const std::string & changes() const;
  const std::vector<std::string> & depsPaths() const;
  std::string addDep(const std::string & p_fileName);

private:
  float m_version;
  std::string m_basedir;
  std::string m_exePath;
  std::vector<std::string> m_exeDepsPaths;
  std::string m_changes;
};

struct PopupUpdateManager : public QThread
{
  Q_OBJECT;

public:
  PopupUpdateManager();
  virtual ~PopupUpdateManager();
  virtual void run();

signals:
  void signal_updateAvailable(float p_version, const QString & p_changes);
  void signal_updateFailed();

public slots:
  void slot_onConnectionUpdate(bool);
  void slot_launchUpdate();
  void slot_exit();

private:
  bool downloadFile(const std::string & p_url,
      const std::string & p_downloadFile);

  bool parseUpdateInfoFile(const std::string & p_path,
                              std::string & p_exeURL,
                              float & p_updateVersion,
                              std::vector<std::string> & p_dependencies,
                              QString & p_changes);

  QMutex m_mutex;
  CURL *m_curl;
  PopupUpdate *m_update;
  bool m_exit;
  QTimer m_timer;
  bool m_connected;
};



#endif /* POPUPUPDATEMANAGER_HPP_ */
